<?php
namespace core;


class SettingsModel extends \think\Model
{
  use \think\model\concern\SoftDelete;

  // 设置settings表，必须有key和value字段
  protected $table = 'settings';

  protected $autoWriteTimestamp = 'datetime';

  protected $deleteTime = 'delete_at';
  protected $createTime = 'create_at';
  protected $updateTime = 'update_at';
}

/**
 * 设置
 */
class Settings
{

  static function set($key,&$value){
    $setting=SettingsModel::where("key",$key)->find();
    $values=serialize($value);
    if ($setting){
      $setting->value=$values;
      $setting->save();
    }else{
      SettingsModel::create([
        "key"=>$key,
        "value"=>$values,
      ]);
    }
    return $values;
  }
  static function get($key,$defaultValue=""){
    $setting=SettingsModel::where("key",$key)->find();
    $result=$defaultValue;
    if ($setting){
      $result=$setting->value;
      $result=unserialize($result);
    }
    return $result;
  }



}
